
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 测试类
 * 
 *
 */
public class MathExam6733 {
	static String mes = ""; // 提示信息
	static Integer n;// 出n道题
	static Integer grade;// 年级
	static int[] num1;// 操作数1
	static int[] num2;// 操作数2
	static int[] op;//

	/**
	 * 主方法
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// A:检查参数
		// B:生成题目
		// C:生成out.txt
		checkArgs(args);
	}

	/**
	 * 检查参数
	 * 
	 * @param args
	 * @return
	 * @throws IOException
	 */
	public static String checkArgs(String[] args) throws IOException {

		// 参数检查
		if (args.length < 2) {
			mes = "请输入两个参数，第一个参数是题目数量，第二个参数是题目难度（1,2）,逗号隔开";
			System.err.println("请输入两个参数，第一个参数是题目数量，第二个参数是题目难度（1,2）,逗号隔开");
			return mes; // 返回提示信息
		}
		n = Integer.parseInt(args[0]);/// 输入：用命令行参数接收两个参数，一个n，表示出n道题
		grade = Integer.parseInt(args[1]);// 另一个grade，表示出年级道题
		// 判断题目数量小于等于0
		if (n <= 0) {
			mes = "请输入题目数量!";
			System.err.println("请输入题目数量!");
			return mes;
		}
		/**
		 * 判断年级是否为1或2或为空
		 */
		if (grade <= 0 || grade != 1 && grade != 2) {
			mes = "请输入（1或2）年级年级!";
			System.err.println("请输入（1或2）年级年级!");
			return mes;
		} else {
			// 创建文件流
			final File f = new File("out6733.txt");
			final FileWriter fw = new FileWriter(f);
			final BufferedWriter bw = new BufferedWriter(fw);
			// 创建字符构造器
			StringBuilder sb = new StringBuilder();
			if (grade == 1) {
				// 生成一年级题目
				gradeOne(bw, n, sb);

			} else if (grade == 2) {
				// 生成二年级题目
				gradeTwo(bw, n, sb);
			}
		}
		return mes;
	}

	/**
	 * 生成out6733.txt
	 * 
	 * @param bw
	 * @param sb
	 * @throws IOException
	 */
	public static void printFile(BufferedWriter bw, StringBuilder sb) throws IOException {
		// 将结果写入文件
		bw.write(sb.toString());
		bw.newLine();
		bw.flush();
		// 关闭文件流
		bw.close();
	}

	/**
	 * 生成一年级的题目
	 * 
	 * @param bw
	 * @param n
	 * @param sb
	 * @throws IOException
	 */
	public static void gradeOne(BufferedWriter bw, Integer n, StringBuilder sb) throws IOException {
		// 2.保存两个操作数
		num1 = new int[n];// 定义一个操作数数组
		num2 = new int[n];// 定义另一个操作数数组
		// 3.保存操作符
		op = new int[n];
		// 根据输入参数为长度，循环随机生成100以内加减法

		for (int i = 0; i < n; i++) {
			num1[i] = new Random().nextInt(100);
			num2[i] = new Random().nextInt(100);
			// 判断两数之和是否大于100或两数之差小于0，若不满足则重新生成
			while ((num1[i] + num2[i]) > 100 || (num1[i] - num2[i] < 0)) {
				num1[i] = new Random().nextInt(100);
				num2[i] = new Random().nextInt(100);

			}
			// 根据随机产生0和1控制运算符
			op[i] = new Random().nextInt(2);
			System.out.print("(" + (i + 1) + ")" + " ");
			sb.append("(" + (i + 1) + ")" + " ");
			// 判断操作符并按格式输出
			if (op[i] == 0) {
				sb.append(num1[i] + " " + "+" + " " + num2[i] + " " + "=\r\n");
				System.out.println(num1[i] + " " + "+" + " " + num2[i] + " " + "=");
			} else {
				sb.append(num1[i] + " " + "-" + " " + num2[i] + " " + "=\r\n");
				System.out.println(num1[i] + " " + "-" + " " + num2[i] + " " + "=");
			}
		}
		System.out.println("----标准答案------");
		sb.append("----标准答案------\r\n");

		// 循环按格式输出题目以及答案
		for (int i = 0; i < n; i++) {
			sb.append("(" + (i + 1) + ")" + " ");
			System.out.print("(" + (i + 1) + ")" + " ");
			if (op[i] == 0) {
				sb.append(num1[i] + " " + "+" + " " + num2[i] + " " + "=" + " " + (num1[i] + num2[i]) + "\r\n");
				System.out.println(num1[i] + " " + "+" + " " + num2[i] + " " + "=" + " " + (num1[i] + num2[i]));
			} else {
				sb.append(num1[i] + " " + "-" + " " + num2[i] + " " + "=" + " " + (num1[i] - num2[i]) + "\r\n");
				System.out.println(num1[i] + " " + "-" + " " + num2[i] + " " + "=" + " " + (num1[i] - num2[i]));
			}
		}
		printFile(bw, sb);
	}

	/**
	 * 生成二年级的题目
	 * 
	 * @param bw
	 * @param n
	 * @param sb
	 * @throws IOException
	 */
	public static void gradeTwo(BufferedWriter bw, Integer n, StringBuilder sb) throws IOException {
		// 2.保存两个操作数
		num1 = new int[n];// 定义一个操作数数组
		num2 = new int[n];// 定义另一个操作数数组
		// 3.保存操作符
		op = new int[n];
		for (int i = 0; i < n; i++) {
			num1[i] = new Random().nextInt(100);
			num2[i] = new Random().nextInt(100);
			// 根据随机产生0和1控制运算符
			op[i] = new Random().nextInt(2);
			System.out.print("(" + (i + 1) + ")" + " ");
			sb.append("(" + (i + 1) + ")" + " ");
			// 判断操作符并按格式输出
			if (op[i] == 0) {
				// 两数乘积小于100，否则重新生成两个数
				while ((num1[i] * num2[i]) > 100) {
					num1[i] = new Random().nextInt(100);
					num2[i] = new Random().nextInt(100);
				}
				sb.append(num1[i] + " " + "*" + " " + num2[i] + " " + "=\r\n");
				System.out.println(num1[i] + " " + "*" + " " + num2[i] + " " + "=");
			} else {
				while (num2[i] == 0 || num2[i] > 10) {
					num2[i] = new Random().nextInt(100);
				}
				sb.append(num1[i] + " " + "/" + " " + num2[i] + " " + "=\r\n");
				System.out.println(num1[i] + " " + "/" + " " + num2[i] + " " + "=");
			}
		}
		System.out.println("----标准答案------");
		sb.append("----标准答案------\r\n");
		// 循环按格式输出题目以及答案
		for (int i = 0; i < n; i++) {
			sb.append("(" + (i + 1) + ")" + " ");
			System.out.print("(" + (i + 1) + ")" + " ");
			if (op[i] == 0) {
				sb.append(num1[i] + " " + "*" + " " + num2[i] + " " + "=" + " " + (num1[i] * num2[i]) + "\r\n");
				System.out.println(num1[i] + " " + "*" + " " + num2[i] + " " + "=" + " " + (num1[i] * num2[i]));
			} else {
				if (num1[i] % num2[i] == 0) {
					sb.append(num1[i] + " " + "/" + " " + num2[i] + " " + "=" + " " + (num1[i] / num2[i]) + "\r\n");
					System.out.println(num1[i] + " " + "/" + " " + num2[i] + " " + "=" + " " + (num1[i] / num2[i]));
				} else {
					sb.append(num1[i] + " " + "/" + " " + num2[i] + " " + "=" + " " + (num1[i] / num2[i]) + "..."
							+ (num1[i] % num2[i]) + "\r\n");
					System.out.println(num1[i] + " " + "/" + " " + num2[i] + " " + "=" + " " + (num1[i] / num2[i])
							+ "..." + (num1[i] % num2[i]));
				}
			}
		}
		printFile(bw, sb);
	}

}
